package com.aqielife.demo.webflux.router;

import com.aqielife.demo.webflux.handler.QuoteHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.http.MediaType.TEXT_PLAIN;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RequestPredicates.contentType;

/**
 * @author aqie
 * @date 2022/02/05 22:49
 * @desc
 */
@Configuration
public class QuoteRouter {

  @Bean
  public RouterFunction<ServerResponse> route(QuoteHandler quoteHandler) {

    return RouterFunctions

    .route(RequestPredicates.GET("/hello")
    .and(RequestPredicates.accept(TEXT_PLAIN)), quoteHandler::hello)

    .andRoute(POST("/echo").and(RequestPredicates.accept(TEXT_PLAIN).and(contentType(TEXT_PLAIN))), quoteHandler::echo)
    //响应一次
    .andRoute(POST("/quotes")
    .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), quoteHandler::fetchQuotes)
    //响应多次
    .andRoute(RequestPredicates.GET("/quotes")
    .and(RequestPredicates.accept(MediaType.APPLICATION_STREAM_JSON)), quoteHandler::streamQuotes);

  }
}
